/**
 * Created by zy on 2016/6/13.
 * 变化趋势/数据对比分析(共用) - 管体缺陷仪表板 - 管体缺陷
 */

//腐蚀控制-阴保运行管理-异常处理

;(function ($, window, document, undefined) {
    var param = $('.exception_id').attr('id');
    DyyBase.ajax(corrctrlApi.url_exception_info(), {exceptionId: param}, function (dataset) {
        var $img_box = $('.lsk-photo');
        if (dataset.result.imageList == "") {
            var title = "报告领导，暂时没有图片";
            var meta = " ";
            var dataImg = DyyBase.defect_empty(title, meta);
            $img_box.html(dataImg);
        } else {
            for (var o in dataset.result.imageList) {
                var $img = $('<img class="lsk-click-img imgP" id="' + dataset.result.imageList[o].id + '" src="' + DyyBase.file_path + dataset.result.imageList[o].path + '"/>');
                $img_box.append($img);
            }
        }
        var ioio = DyyBase.getImagePath(dataset.result.imageList);
        $('img.lsk-click-img').click(function () {
            var id = $(this).attr('id');
            $('#JQ_img_div').loadImg(ioio, id);
        });
    });

    // 开关切换
    $('.off-on').on('click', function () {
        if ($(this).hasClass('active')) {
            $(this).removeClass('active')
            $(this).attr({"data_value": "UNTREATED"})
        } else {
            $(this).addClass('active')
            $(this).attr({"data_value": "PROCESSED"})
        }
    });
    //  已处理情况
    var $set = $('#onSet').attr("data-value");
    if ($set == "已处理") {
        $('.off-on').off('click');
        $('#JQ_submit_01').addClass('none')
    }
    //  确认事件
    $('#JQ_submit_01').on('click', function () {
        var $id = $('.exception_id').attr('id');
        var $clyj_value = $('#clyj').val();
        var $czjl_value = $('#czjl').val();
        var $state = $('.off-on').attr('data_value');
        var OBJ = {};
        OBJ.id = $id;
        OBJ.stated = $state;
        OBJ.treatOpinion = $clyj_value;
        OBJ.treatRecord = $czjl_value;

        var $this = $(this);
        DyyBase.ajax(corrctrlApi.url_ok(), OBJ, function (dataset) {
            if (dataset.bool) {
                $this.MSG({
                    type: 'success',
                    content: '保存成功',
                    yesBtnAct: function () {
                        window.location.href = DyyBase.root_path + '/corrctrl/eqptException/recoedException.html'
                    }
                });
            } else {
                $this.MSG({
                    type: 'error',
                    content: dataset.result
                });
            }

        });
    });
    var filter = {
        // 导出
        getTowExport: function (data) {
            var filter = {};
            // 分页参数
            if (data) {
                if (data.start == 0) {
                    filter['jsonBean.pageNum'] = 1;
                } else {
                    filter['jsonBean.pageNum'] = data.start / 10 + 1;
                }
                filter['jsonBean.pageCount'] = data.length;
            }
            // 查询参数
            filter.pipeLineName = function () {
                var v = $('#pipeLineName').text();
                if (v) {
                    return v;
                }
                return '';
            }();
            return filter;
        },

        getExport: function () {
            return filter.getTowExport();
        },
    };
    $('#JQ_export_btn_01').click(function () {
        var param = filter.getExport;
        var namep = $("#pname").attr('value')
        var facilityType = $('#sbnx').attr('value');
        var facilityNo = $('#sbbh').attr('value');

        if (param instanceof Function) {
            param = param();
        }
        var fileNames = namep + facilityType + facilityNo + '异常处理单';
        $(this).EXPT({
            type: 'name',
            name: param.pipeLineName + facilityType + facilityNo + '异常处理单',
            param: param,
            server: DyyBase.root_path + '/comm/downImgPdf.json',
            yesBtnAct: function (callback, fname) {
                callback()
                var $ifr_dyMap = $('#JQ_dyMap');
                var $ifr_body = $($ifr_dyMap[0].contentWindow.document.getElementById('container'));

                /*
                 var html1_1 = $ifr_body.html();
                 var _svg_html = html1_1.substring(html1_1.indexOf("<svg"));
                 //console.log(_svg_html);
                 var $t_canvas = $('<canvas id="t_canvas"></canvas>');
                 $('#t_canvas_div').append($t_canvas);
                 canvg("t_canvas", _svg_html);
                 var _svg = $ifr_dyMap[0].contentWindow.document.getElementById('mask_gc');
                 $(_svg).remove();//删掉svg
                 $ifr_body.append($t_canvas);//画布填充到iframe
                 $('#t_canvas_div').empty();//*/
                html2canvas($ifr_body, {
                    proxy: DyyBase.root_path + '/comm/downNetFile.json',
                    onrendered: function (canvas) {
                        var url = canvas.toDataURL();
                        //$("#imgaaa").attr('src', url);
                        var $img = $('<img style="width: 100%; height: 100%">').attr('src', url);
                        $ifr_dyMap.after($img).hide();//appendTo($('#t_canvas_div'))

                        html2canvas($('.conter'), {
                            proxy: DyyBase.root_path + '/comm/downNetFile.json',
                            onrendered: function (canvas) {
                                $ifr_dyMap.show();
                                $img.remove();

                                var url = canvas.toDataURL();
                                var param = [{name: 'fileName', value: fname}, {
                                    name: 'name',
                                    value: '异常管理_异常处理单'
                                }, {name: 'imgs', value: url}];
                                s_downFile(DyyBase.root_path + '/comm/downImgPdf.json', param);

                            }
                        });
                    }
                });

                $('.zy-action-box').css({display: 'none'});
                $('.zy-msg-mask').css({display: 'none'});
            }
        });

    });
})(jQuery, window, document);